//
//  ZSLVisitorView.swift
//  micro_blog
//
//  Created by Mac on 16/9/21.
//  Copyright © 2016年 Mac. All rights reserved.
//访客视图

import UIKit

protocol ZSLVisitorViewDelegate: NSObjectProtocol
{
    
    func didSelectedLogin()
    
}

class ZSLVisitorView: UIView {

    //闭包
    var loginClosure: (()->())?
    
    weak var delegate: ZSLVisitorViewDelegate?
    
    //懒加载控件
   //旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //添加罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //主页图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //未登录信息
    private lazy var messageLable: UILabel = {
       
        let lable = UILabel()
        
        lable.font = UIFont.systemFont(ofSize: 13)
        
        lable.textColor = UIColor.darkGray
        
        lable.textAlignment = .center
        
        lable.numberOfLines = 0
        
        return lable
        
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
       
        let button = UIButton()
        
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        button.setTitle("登录", for: .normal)
        
        button.setTitleColor(UIColor.darkGray, for: .normal)
        
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        
        //去掉点击高粱效果
        
        button.adjustsImageWhenHighlighted = false
        
        return button
        
    }()
    
    //添加注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        button.setTitle("注册", for: .normal)
        
        button.setTitleColor(UIColor.darkGray, for: .normal)
        
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        
        //去掉点击高粱效果
        
        button.adjustsImageWhenHighlighted = false
        
        return button
        
    }()

    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(cycleImageView)
        
        addSubview(maskImageView)
        
        addSubview(iconImageView)
        
        addSubview(messageLable)
        
        addSubview(loginButton)
        
        addSubview(registerButton)
        
        cycleImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLable.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLable.snp_leading)
            make.top.equalTo(messageLable.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLable.snp_trailing)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }
        
        
    }
    
    
    func updateVisitorInfo(message: String?,imageName: String?) {
        
        if let mas = message, let imageName = imageName
        {
            messageLable.text = mas
            
            iconImageView.image = UIImage(named: imageName)
            
            cycleImageView.isHidden = true
            
        }else
        {
            startAnimation()
            
        }
        
        
        }
    
    
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        animation.isRemovedOnCompletion = false
        
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    

    
    
    
    
    
    
    @objc private func loginAction() {
        
        print("我是访客视图点击的操作")
        
        loginClosure?()
        
        //执行代理方法
        delegate?.didSelectedLogin()
    }
    
}



